package com.example.micro_property_springboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.micro_property_springboot.pojo.SysOperLog;
import com.example.micro_property_springboot.mapper.SysOperLogMapper;
import com.example.micro_property_springboot.pojo.vo.OperLogQueryVO;
import com.example.micro_property_springboot.service.SysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 操作日志服务实现
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog>
        implements SysOperLogService {
    
    @Autowired
    private SysOperLogMapper operLogMapper;
    
    @Override
    @Transactional
    public boolean saveOperLog(SysOperLog operLog) {
        // 设置操作时间
        if (operLog.getOperTime() == null) {
            operLog.setOperTime(LocalDateTime.now());
        }

        
        return save(operLog);
    }
    
    @Override
    public Map<String, Object> queryOperLogWithPage(OperLogQueryVO query, Integer pageNum, Integer pageSize) {
        // 计算起始索引
        Integer startIndex = (pageNum - 1) * pageSize;
        
        // 查询数据
        List<SysOperLog> records = operLogMapper.queryOperLogWithPage(query, startIndex, pageSize);
        
        // 查询总数
        Integer total = operLogMapper.queryOperLogCount(query);
        
        // 封装结果
        Map<String, Object> result = new HashMap<>();
        result.put("records", records);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        result.put("pages", (total + pageSize - 1) / pageSize);
        
        return result;
    }
    
    @Override
    @Transactional
    public boolean deleteOperLog(Long id) {
        return removeById(id);
    }
    
    @Override
    @Transactional
    public boolean batchDeleteOperLogs(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return true;
        }
        return removeByIds(ids);
    }
    
    @Override
    @Transactional
    public boolean clearOperLogs() {
        return operLogMapper.truncateOperLog() > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteOldOperLogs(String date) {
        return operLogMapper.deleteOldOperLogs(date) > 0;
    }
    
    @Override
    public Map<String, Object> getOperLogStatistics() {
        Map<String, Object> statistics = new HashMap<>();
        
        // 查询总日志数
        Integer totalCount = Math.toIntExact(count());
        statistics.put("totalCount", totalCount);
        
        // 查询成功日志数
        Integer successCount = Math.toIntExact(lambdaQuery().eq(SysOperLog::getOperType, "新增").count());
        statistics.put("successCount", successCount);
        
        // 查询失败日志数
        Integer failCount = Math.toIntExact(lambdaQuery().eq(SysOperLog::getOperType, "删除").count());
        statistics.put("failCount", failCount);
        
        // 查询操作类型统计
        statistics.put("operTypeStatistics", getOperTypeStatistics());
        
        // 查询模块访问统计
        statistics.put("moduleAccessStatistics", getModuleAccessStatistics());
        
        return statistics;
    }
    
    @Override
    public List<Map<String, Object>> getOperTypeStatistics() {
        // 这里使用简单的方式统计，实际项目中可以通过SQL查询优化
        List<Map<String, Object>> statistics = new ArrayList<>();
        
        // 常见操作类型
        String[] operTypes = {"新增", "修改", "删除", "查询", "导出", "导入", "登录", "登出", "其他"};
        
        for (String operType : operTypes) {
            Long count = lambdaQuery().eq(SysOperLog::getOperType, operType).count();
            
            if (count > 0) {
                Map<String, Object> statItem = new HashMap<>();
                statItem.put("operType", operType);
                statItem.put("count", count);
                statistics.add(statItem);
            }
        }
        
        // 按数量降序排序
        statistics.sort((a, b) -> Long.compare((Long) b.get("count"), (Long) a.get("count")));
        
        return statistics;
    }
    
    @Override
    public List<Map<String, Object>> getModuleAccessStatistics() {
        // 这里使用简单的方式统计，实际项目中可以通过SQL查询优化
        List<Map<String, Object>> statistics = new ArrayList<>();
        
        // 查询所有不重复的模块
        List<String> modules = operLogMapper.queryDistinctModules();
        
        for (String module : modules) {
            Long count = lambdaQuery().eq(SysOperLog::getOperModule, module).count();
            
            Map<String, Object> statItem = new HashMap<>();
            statItem.put("module", module);
            statItem.put("count", count);
            statistics.add(statItem);
        }
        
        // 按数量降序排序
        statistics.sort((a, b) -> Long.compare((Long) b.get("count"), (Long) a.get("count")));
        
        return statistics;
    }
}